/**
******************************************************************************
* @file    			main.c
* @version 			V1.0
* @date    			2024-08-21
* @brief 		    Main function entry
*
* @company  		WIZnet
* @website  		https://wiznet.io/
* @gitee            https://gitee.com/wiznet-hk
* @github           https://github.com/Wiznet/
******************************************************************************
*/

#include "bsp_rcc.h"
#include "bsp_tim.h"
#include "bsp_uart.h"
#include "delay.h"
#include "do_co2.h"
#include "do_mqtt.h"
#include "do_pm25.h"
#include "http_server.h"
#include "w55mh32_wztoe.h"
#include "wiz_interface.h"
#include "wizchip_conf.h"
#include <stdint.h>
#include <stdio.h>
// #include <stdlib.h>
// #include <string.h>

#define SOCKET_ID 0
#define ETHERNET_BUF_MAX_SIZE (1024 * 2)

/* network information */
wiz_NetInfo g_default_net_info = {.mac = {0x00, 0x08, 0xdc, 0x12, 0x22, 0x12},
                                  .ip = {192, 168, 1, 30},
                                  .gw = {192, 168, 1, 1},
                                  .sn = {255, 255, 255, 0},
                                  .dns = {8, 8, 8, 8},
                                  .dhcp = NETINFO_DHCP};

uint8_t g_ethernet_buf[ETHERNET_BUF_MAX_SIZE] = {0};
static uint8_t g_mqtt_send_ethernet_buf[ETHERNET_BUF_MAX_SIZE] = {0};
static uint8_t g_mqtt_recv_ethernet_buf[ETHERNET_BUF_MAX_SIZE] = {0};

int main(void) {
  /* hardware initialization */
  rcc_clk_config();
  delay_init();

  console_usart_init(115200);
  pm25_usart2_init(9600);
  co2_usart3_init(9600);
  tim3_init();

  printf("%s MQTT Aliyun example\r\n", _WIZCHIP_ID_);

  /* wiztoe init */
  wiz_toe_init();

  wiz_phy_link_check();

  network_init(g_ethernet_buf, &g_default_net_info);

  //  http_server_init(1, 80); // 端口80

  mqtt_init(SOCKET_ID, g_mqtt_send_ethernet_buf, g_mqtt_recv_ethernet_buf);

  while (1) {
    do_mqtt();
    pm25_data_process_usart2();
    co2_data_process_usart3();
    loopback_tcps(1, g_ethernet_buf, 80);
  }
}
